package com.ruoyi.app.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.app.domain.AppDonations;
import com.ruoyi.app.service.IAppDonationsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的捐款记录Controller
 * 
 * @author ruoyi
 * @date 2025-11-06
 */
@RestController
@RequestMapping("/app/my-donations")
public class AppMyDonationsController extends BaseController
{
    @Autowired
    private IAppDonationsService appDonationsService;

    /**
     * 获取用户捐款记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(
        @RequestParam(value = "status", required = false) String status,
        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
    {
        startPage();
        
        // 创建查询条件
        AppDonations queryDonations = new AppDonations();
        if (status != null && !status.equals("all")) {
            queryDonations.setPaymentStatus(status);
        }
        
        // 这里应该根据当前登录用户ID查询，暂时查询所有
        // Long userId = getUserId(); // 获取当前登录用户ID
        // queryDonations.setUserId(userId); // 需要在AppDonations中添加userId字段
        
        List<AppDonations> list = appDonationsService.selectAppDonationsList(queryDonations);
        return getDataTable(list);
    }

    /**
     * 获取捐款统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        try {
            // 这里应该根据当前登录用户ID查询统计信息
            // Long userId = getUserId();
            
            // 创建查询条件
            AppDonations queryDonations = new AppDonations();
            queryDonations.setPaymentStatus("completed"); // 只统计已完成的捐款
            
            List<AppDonations> completedDonations = appDonationsService.selectAppDonationsList(queryDonations);
            
            // 计算统计数据
            int totalDonations = completedDonations.size();
            BigDecimal totalAmount = completedDonations.stream()
                .map(donation -> donation.getAmount() != null ? donation.getAmount() : BigDecimal.ZERO)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
            
            Map<String, Object> statistics = new HashMap<>();
            statistics.put("totalDonations", totalDonations);
            statistics.put("totalAmount", totalAmount);
            
            return success(statistics);
        } catch (Exception e) {
            logger.error("获取捐款统计信息失败", e);
            return error("获取统计信息失败");
        }
    }

    /**
     * 获取捐款记录详情
     */
    @GetMapping("/detail")
    public AjaxResult getDetail(@RequestParam("donationId") Long donationId)
    {
        AppDonations donation = appDonationsService.selectAppDonationsByDonationId(donationId);
        if (donation == null) {
            return error("捐款记录不存在");
        }
        
        // 这里可以添加权限检查，确保用户只能查看自己的捐款记录
        // Long userId = getUserId();
        // if (!donation.getUserId().equals(userId)) {
        //     return error("无权查看此捐款记录");
        // }
        
        return success(donation);
    }
}
